using System;
using System.Collections.Generic;
using UnityEngine;

public class BattleUI : MonoBehaviour
{
	private GameObject uiobj;

	public UIScrollView scrollview;

	public UIScrollBar scrollbar;

	public UIEventListener lastbtn;

	public UIWidget lastwi;

	public UIEventListener nextbtn;

	public UIWidget nextwi;

	public UILabel allstarlabel;

	public List<UIEventListener> islandbtns;

	public List<UITexture> islandflags;

	public List<UIWidget> starwis;

	public List<UILabel> starlabels;

	public List<UIWidget> requirewi;

	public List<UILabel> requirelabel;

	public UIWidget confirmwi;

	public Animator animator;
	//public Animation confirmani;

	public UIEventListener buybtn;

	public UIEventListener closebtn;

	public UILabel reqlabel;

	public UILabel spendnumlabel;

	private Color lockcolor = new Color(20f / 51f, 20f / 51f, 20f / 51f);

	private int[] levelinfo;

	private List<levellock> levellockinfo;

	private int cur_level_num;

	private int next_level;

	private bool scrollmoveflag;

	private float starttime;

	private float intaltime;

	private float endval;

	private float scrollrate;

	private float scrollstep = 0.2f;

	private float[] initbar = new float[8]
	{
		0f,
		0.04f,
		0.04f,
		0.21f,
		0.446f,
		0.627f,
		1f,
		1f
	};

	private int conformindex;

	private bool isshowconform;

	private void Start()
	{
		for (int i = 0; i < islandbtns.Count; i++)
		{
			UIEventListener uIEventListener = islandbtns[i];
			uIEventListener.onClick = (UIEventListener.VoidDelegate)Delegate.Combine(uIEventListener.onClick, new UIEventListener.VoidDelegate(OnClickisland));
		}
		lastbtn.onClick = OnClicklastBtn;
		nextbtn.onClick = OnClicknextBtn;
		buybtn.onClick = OnClickbuyBtn;
		closebtn.onClick = OnClickcloseBtn;
		Invoke("initscroll", 2f * Time.deltaTime);
	}

	public void initscroll()
	{
		scrollbar.value = -1f;
		scrollbar.value = initbar[cur_level_num / 6];
	}

	private void Update()
	{
		if (scrollmoveflag)
		{
			if (scrollview.isbepress)
			{
				scrollmoveflag = false;
				return;
			}
			if (Time.time - starttime < intaltime)
			{
				scrollbar.value += Time.deltaTime * scrollrate;
				return;
			}
			scrollbar.value = endval;
			scrollmoveflag = false;
		}
	}

	public void init_ui_info()
	{
		MenuPlayerprefs.instance.readAllLevel();
		levelinfo = new int[8];
		levelinfo = MenuPlayerprefs.instance.biglevelinfo;
		cur_level_num = MenuPlayerprefs.instance.curgamelevel;
		next_level = MenuPlayerprefs.instance.nextlevel;
		levellockinfo = MenuDataController.instance.levellockinfo;
		for (int i = 0; i < 8; i++)
		{
			changelevel(i);
		}
		scrollbar.value = initbar[cur_level_num / 6];
		allstarlabel.text = string.Empty + MenuPlayerprefs.instance.GetAllstar();
	}

	public void changelevel(int id)
	{
		if (levelinfo[id] / 1000000 >= 1)
		{
			islandflags[id].color = Color.white;
			requirewi[id].alpha = 0f; requirewi[id].gameObject.SetActive(false);
			starwis[id].alpha = 1f; starwis[id].gameObject.SetActive(true);
			starlabels[id].text = string.Empty + callevel_star(levelinfo[id]) + "/18";
		}
		else if (cur_level_num >= id * 6)
		{
			islandflags[id].color = Color.white;
			requirewi[id].alpha = 1f; requirewi[id].gameObject.SetActive(true);
			starwis[id].alpha = 0f; starwis[id].gameObject.SetActive(false);
			requirelabel[id].text = "要求 " + levellockinfo[id].maxstar;
		}
		else
		{
			islandflags[id].color = lockcolor;
			requirewi[id].alpha = 1f; requirewi[id].gameObject.SetActive(true);
			starwis[id].alpha = 0f; starwis[id].gameObject.SetActive(false);
			requirelabel[id].text = "要求 " + levellockinfo[id].maxstar;
		}
	}

	public void AutoEnterLevel()
	{
		if (levelinfo[next_level / 6] / 1000000 == 1)
		{
			island_click(next_level / 6);
		}
	}

	public void AutoEnterFexLevel(int levelid)
	{
		if (levelinfo[levelid / 6] / 1000000 == 1)
		{
			island_click(levelid / 6);
		}
	}

	public int callevel_star(int num)
	{
		num %= 1000000;
		if (num < 10)
		{
			return num;
		}
		int num2 = 0;
		while (num > 0)
		{
			num2 += num % 10;
			num /= 10;
		}
		return num2;
	}

	public void ChangeScroll()
	{
		if (scrollbar.value < 0.01f)
		{
			lastwi.alpha = 0f; lastwi.gameObject.SetActive(false);
		}
		else if (lastwi.alpha != 1f)
		{
			lastwi.alpha = 1f; lastwi.gameObject.SetActive(true);
		}
		if (scrollbar.value > 0.99f)
		{
			nextwi.alpha = 0f; nextwi.gameObject.SetActive(false);
		}
		else if (nextwi.alpha != 1f)
		{
			nextwi.alpha = 1f; nextwi.gameObject.SetActive(true);
		}
	}

	public void OnClicklastBtn(GameObject btn)
	{
		if (!scrollmoveflag && scrollbar.value != 0f)
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
			scrollmoveflag = true;
			starttime = Time.time;
			intaltime = 0.5f;
			scrollrate = -0.4f;
			endval = scrollbar.value - scrollstep;
			if (scrollbar.value < scrollstep)
			{
				intaltime = scrollbar.value / scrollstep * intaltime;
				endval = 0f;
			}
		}
	}

	public void OnClicknextBtn(GameObject btn)
	{
		if (!scrollmoveflag && scrollbar.value != 1f)
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
			scrollmoveflag = true;
			starttime = Time.time;
			intaltime = 0.5f;
			scrollrate = 0.4f;
			endval = scrollbar.value + scrollstep;
			if (1f - scrollbar.value < scrollstep)
			{
				intaltime = (1f - scrollbar.value) / scrollstep * intaltime;
				endval = 1f;
			}
		}
	}

	public void OnClickisland(GameObject btn)
	{
		int num = 0;
		while (true)
		{
			if (num < islandbtns.Count)
			{
				if (btn.name.Equals("btn" + (num + 1)))
				{
					break;
				}
				num++;
				continue;
			}
			return;
		}
		island_click(num);
	}

	public void island_click(int num)
	{
		if (levelinfo[num] / 1000000 == 0)
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_pophit, 0f);
			if (cur_level_num >= num * 6)
			{
				conformindex = num;
				isshowconform = true;
				confirmwi.alpha = 1f; confirmwi.gameObject.SetActive(true);
				/* confirmani["PopWinAnimation"].time = 0f;
				confirmani["PopWinAnimation"].speed = 1f;
				confirmani.Play(); */
				animator.SetTrigger("popWin1");
				MenuBackController.instance.ChangeMenuUIstate(MenuUIState.confirm_unlocklevel);
				reqlabel.text = "要求 " + levellockinfo[num].maxstar;
				spendnumlabel.text = "花费 " + levellockinfo[num].moneybuy;
			}
			else
			{
				MenuUIController.instance.enable_hitinfoui(hitinfoid.battlelock);
			}
		}
		else
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
			MenuUIController.instance.enable_levelui(num);
			disable();
		}
	}

	public void OnClickbuyBtn(GameObject btn)
	{
		if (MenuPlayerprefs.instance.DecMoney(levellockinfo[conformindex].moneybuy))
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BUY, 0f);
			MenuPlayerprefs.instance.Setlevelunlock();
			levelinfo[conformindex] += 1000000;
			changelevel(conformindex);
			MenuBackController.instance.BackMenuUIstate();
			Platform.flurryEvent_buylevel(conformindex);
		}
		else
		{
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN1, 0f);
			MenuUIController.instance.enable_chargeui(0);
		}
		confirmwi.alpha = 0f; confirmwi.gameObject.SetActive(false);
	}

	public void OnClickcloseBtn(GameObject btn)
	{
		if (isshowconform)
		{
			isshowconform = false;
			MonoInstance<AudioController>.instance.play(AudioType.UI_BTN, 0f);
			/* confirmani["PopWinAnimation"].time = confirmani.clip.length;
			confirmani["PopWinAnimation"].speed = -1f;
			confirmani.Play(); */
			animator.SetTrigger("popWin2");
			Invoke("confirmClose", animator.runtimeAnimatorController.animationClips[0].length);
		}
	}

	public void confirmClose()
	{
		confirmwi.alpha = 0f; confirmwi.gameObject.SetActive(false);
		MenuBackController.instance.BackMenuUIstate();
	}

	public void enable()
	{
		if (uiobj == null)
		{
			uiobj = base.gameObject;
		}
		NGUITools.SetActive(uiobj, state: true);
		init_ui_info();
	}

	public void disable()
	{
		NGUITools.SetActive(uiobj, state: false);
	}
}
